package org.vectrics.recruiter.struts.tag;

import java.util.Iterator;
import java.util.List;

import org.vectrics.SystemException;
import org.vectrics.common.html.BorderTable;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Form;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.TextHelper;
import org.vectrics.common.pagelist.PageList;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.Job;
import org.vectrics.recruiter.job.JobCandidate;
import org.vectrics.recruiter.user.CandidateInfo;
import org.vectrics.recruiter.user.Interview;
import org.vectrics.recruiter.user.InterviewCriteria;
import org.vectrics.struts.PageListTag;
import org.vectrics.user.Person;


public class PersonInterviewListTag extends PageListTag {

	private static final long serialVersionUID = 876555497073110753L;
	
	private String candidateId = null;
	private String width = "80%";
    public static String TABLE_FONT = "subFont";

	/* (non-Javadoc)
	 * @see org.vectrics.struts.PageListTag#getDescription()
	 */
	protected String getDescription() {
		return null;
	}

	/* (non-Javadoc)
	 * @see org.vectrics.struts.PageListTag#getPageListLink()
	 */
	protected String getPageListLink() {
		return (getContextPath() + "/admin/user/interview/list.do");
	}

	/* (non-Javadoc)
	 * @see org.vectrics.struts.PageListTag#showList(java.lang.String, org.vectrics.common.html.Cell, java.util.Collection)
	 */
	protected void showList(String description, Cell mainCell,
			List elements, Form form) {
		BorderTable table = new BorderTable();
		table.setWidth(this.width);
		mainCell.addElement(table);
		
		Row hdrRow = table.addRow();
        hdrRow.setDefaultBgImage(this.getContextPath() + "/site-graphics/tbl_hdr.gif");

        Cell hdrCell = null;
		
		hdrCell = hdrRow.addCell();
//        hdrCell.setClass("tableHdrBg");
        hdrCell.addElement(TextHelper.getFont("tableHdrFont", this.getPhrase("prompt.interview.date.start")));
        hdrCell.setWidth(90);
        hdrCell.setNoWrap(true);
		
		hdrCell = hdrRow.addCell();
//        hdrCell.setClass("tableHdrBg");
        hdrCell.addElement(TextHelper.getFont("tableHdrFont", this.getPhrase("prompt.interview.interviewer")));
        hdrCell.setWidth("30%");
		
		hdrCell = hdrRow.addCell();
//        hdrCell.setClass("tableHdrBg");
        hdrCell.addElement(TextHelper.getFont("tableHdrFont", this.getPhrase("prompt.interview.status")));
        hdrCell.setWidth("30%");
		
		hdrCell = hdrRow.addCell();
//        hdrCell.setClass("tableHdrBg");
        hdrCell.addElement(TextHelper.getFont("tableHdrFont", this.getPhrase("prompt.interview.job.title")));
        hdrCell.setWidth("30%");
		
		hdrCell = hdrRow.addCell();
//        hdrCell.setClass("tableHdrBg");
        hdrCell.addElement(TextHelper.getFont("tableHdrFont", this.getPhrase("prompt.interview.candidate.status")));
        hdrCell.setWidth("30%");
        hdrCell.setNoWrap(true);

		Iterator iter = elements.iterator();
		while (iter.hasNext()) {
			Interview interview = (Interview)iter.next();
	        CandidateInfo candidateInfo = RecruitServiceLocator.getRecruitUserService().findCandidateInfo(interview.getPerson());
			Row row = table.addRow();
			Cell cell = null;
			
			// Interview Time
			cell = row.addCell();
			cell.setVAlign("top");
			cell.addElement(TextHelper.getFont(TABLE_FONT, CoreServiceLocator.getDateFormat().getDateFormatRoundToMinute(this.getLocale()).format(interview.getDateStart())));
			

			// Interviewer
			cell = row.addCell();
			cell.setVAlign("top");
			cell.addElement(TextHelper.getFont(TABLE_FONT, interview.getLeadPerson().getName()));			
			
			// Interview status
			cell = row.addCell();
			cell.setVAlign("top");
			if (interview.getLeadPerson() != null)
				cell.addElement(TextHelper.getFont(TABLE_FONT, interview.getStatusType().getName()));			

			// job Title
			cell = row.addCell();
			cell.setVAlign("top");
			if (interview.getJob() != null)
				cell.addElement(TextHelper.getFont(TABLE_FONT, interview.getJob().getTitle()));
			
			// Job candidate status
			cell = row.addCell();
			cell.setVAlign("top");
			if (interview.getJob() != null) {
				Job job = interview.getJob();
				if (job != null) {
					JobCandidate jobCandidate = job.findJobCandidate(candidateInfo);
					if (jobCandidate == null)
						throw new SystemException("Could not find candidate for job, interview indicates this person is a candidate for job, but there is no job_candidate entry.");
					cell.addElement(TextHelper.getFont(TABLE_FONT, jobCandidate.getCurrentStatus().getStatusType().getDescription()));
				}
			}
		}
	}

	/* (non-Javadoc)
	 * @see org.vectrics.struts.PageListTag#createPageList()
	 */
	protected PageList createPageList() {
		Long candidateId = new Long(this.candidateId);
		Person candidate = CoreServiceLocator.getPersonService().findPersonById(candidateId);
		
		InterviewCriteria criteria = new InterviewCriteria();
		criteria.setInterviewer(candidate);
		PersonInterviewPageList pageList = new PersonInterviewPageList(criteria);
		
		return pageList;
	}

	/**
	 * @return Returns the personId.
	 */
	public String getCandidateId() {
		return candidateId;
	}
	/**
	 * @param personId The personId to set.
	 */
	public void setCandidateId(String personId) {
		this.candidateId = personId;
	}
	/**
	 * @return Returns the width.
	 */
	public String getWidth() {
		return width;
	}
	/**
	 * @param width The width to set.
	 */
	public void setWidth(String width) {
		this.width = width;
	}
}
